package com.tongyi.lizhishop.activitys;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.beans.PublishedRedPackBean;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.common.RecycleViewActivity;
import com.tongyi.lizhishop.constants.Constants;
import com.tongyi.lizhishop.managers.APIServiceManager;
import com.tongyi.lizhishop.utils.AppStringUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class RedEnvelopeManager extends RecycleViewActivity implements View.OnClickListener, OnRefreshListener, OnLoadmoreListener {

    ArrayList<PublishedRedPackBean.ArrayBean> itemDatas = new ArrayList();
    Button sendRedEnvelope;
    private CommonAdapter adapter;
    boolean isShowCheckButton = false;
    private TextView rightTextView;
    private int currentPageIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitleBarView(titlebar, "我发布的红包");
        initView();
        registerBrocast();
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshLayout.autoRefresh();
    }

    private void registerBrocast() {
    }

    @Override
    public void loadData() {
        APIServiceManager.getInstance().create(MessageApi.class)
                .showCard(SPUtils.getInstance().getString("userid"), Constants.perPageSize, currentPageIndex)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CommonObserver<PublishedRedPackBean>() {
                    @Override
                    public void onSuccess(PublishedRedPackBean value) {
                        onActivitySuccess(adapter, itemDatas, value.getArray());
                        LogUtils.w(value);
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                        showError("请求失败");
                    }
                });


    }


    private void initView() {
        getLayoutInflater().inflate(R.layout.activity_my_iccard_bottomview, footer, true);
        sendRedEnvelope = (Button) findViewById(R.id.btnIcCard);
        sendRedEnvelope.setText("发布红包");
        sendRedEnvelope.setOnClickListener(this);
        rightTextView = titlebar.getRightTextView();
//        rightTextView.setText("删除");
        rightTextView.setOnClickListener(this);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setItemAnimator(new DefaultItemAnimator());
        adapter = new CommonAdapter<PublishedRedPackBean.ArrayBean>(this, R.layout.activity_my_publish_red_envelope_item, itemDatas) {
            @Override
            protected void convert(ViewHolder holder, PublishedRedPackBean.ArrayBean o, final int position) {
                String car_name = o.getCar_name();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm ");
                String car_startTime = "";
                String car_endTime = "";

                car_startTime = AppStringUtils.getSafeTime("yyyy-MM-dd HH:mm", o.getCar_starttime());
                car_endTime = AppStringUtils.getSafeTime("yyyy-MM-dd HH:mm", o.getCar_endtime());
                String dmoney = "满" + o.getCar_dmoney() + "可用";
                String jmoney = o.getCar_jmoney() + "元";
                String car_state = AppStringUtils.getStateString(o.getCar_state());

                holder.getView(R.id.deleteCheckBtn).setVisibility(View.GONE);
                String b = "" + o.getCar_num();
                String a = "" + o.getCar_numed();
                holder.setText(R.id.redmoneycount, "已领" + a + "个,共" + b + "个");
                ((TextView) holder.getView(R.id.car_state)).setText(car_state);
                ((TextView) holder.getView(R.id.car_dmoney)).setText(dmoney);
                ((TextView) holder.getView(R.id.car_jmoney)).setText("面额:" + jmoney);
                holder.setText(R.id.busnessRedEnvelop, car_name);
                ((TextView) holder.getView(R.id.car_starttime)).setText(car_startTime + "--");
                ((TextView) holder.getView(R.id.car_endtime)).setText(car_endTime);

           /*     ((CheckBox) holder.getView(R.id.deleteCheckBtn)).setChecked(itemDatas.get(position).isChecked());
                ((CheckBox) holder.getView(R.id.deleteCheckBtn)).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Item item = itemDatas.get(position);
                        item.setChecked(!item.isChecked());
                        adapter.notifyDataSetChanged();

                    }
                });*/
            }
        };
        recyclerview.setAdapter(adapter);
        initRefreshLayout();

    }

    private void initRefreshLayout() {
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadmoreListener(this);
        refreshLayout.setEnableAutoLoadmore(true);
    }

    private void showCheckStatus() {
        rightTextView.setText("完成");
        sendRedEnvelope.setText("删除");
        adapter.notifyDataSetChanged();
    }

    private void resetCheckStatus() {
        rightTextView.setText("删除");
        sendRedEnvelope.setText("发布红包");
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnIcCard) {
            //发布红包
            onViewClicked();
            return;
        }
        if (isShowCheckButton) {
            resetCheckStatus();
        } else {
            showCheckStatus();

        }
        isShowCheckButton = !isShowCheckButton;
    }

    public void onViewClicked() {
        if (isShowCheckButton) {
            //删除状态;
           /* for (int i = itemDatas.size() - 1; i >= 0; i--) {
                if (itemDatas.get(i).isChecked()) {
                    itemDatas.remove(i);
                    adapter.notifyItemRemoved(i);
                    adapter.notifyItemRangeChanged(i, itemDatas.size() - i);
                }
            }*/

          /*  Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
                @Override
                public boolean queueIdle() {

                    resetCheckStatus();
                    return false;
                }
            });*/
        } else {
            ActivityUtils.startActivity(this, PublishRedEnvelopeActivity.class);
        }
    }


    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        currentPageIndex = 0;
        loadData();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        loadData();
    }
}
